<template>
  <div class="addroomy">
    <hunk-head name="添加室友"></hunk-head>
    <div class="addform">
      <div class="form-item">
        <div class="name">是否匿名</div>
        <div class="add-input">
          <!-- <uni-forms-item name="isanonymous" label="是否匿名" required :label-width="120"> -->
          <uni-data-checkbox selectedColor="#FFBD6C"  selectedTextColor="#4A4A4A" v-model="addInfo.isanonymous" :localdata="isanonymous"/>
        <!-- </uni-forms-item> -->
          <!-- <input type="text" placeholder="请输入室友姓名" v-model="addInfo.roommatename"> -->
        </div>
      </div>
      <div class="space"></div>
      <div class="form-item" >
        <div class="name">姓名</div>
        <div class="add-input">
          <input :disabled="addInfo.isanonymous" type="text" placeholder="请输入室友姓名" v-model="addInfo.roommatename">
        </div>
      </div>
      <div class="space"></div>
      <div class="form-item">
        <div class="name">身份证</div>
        <div class="add-input">
          <input :disabled="addInfo.isanonymous" type="text" placeholder="请输入室友身份证"  v-model="addInfo.roommatecardid">
        </div>
      </div>
    </div>
    <div class="addbtn" @click="addRoomy" >
      生成室友链接
    </div>
    <uni-popup ref="popup" type="center">
      <div class="popup1">
        <div class="con-head">
          室友链接生成成功，是否分享好友
          <img @click="closePupop()" v-if="baseUrl" :src="baseUrl+'order/close.png'" alt="" srcset="">
        </div>
        <button class="btn1" open-type="share">确定</button>
        <div class="btn2 hui" @click="closePupop()">取消</div>
      </div>
    </uni-popup>
  </div>
</template>

<script>
import Head from '@componentsMine/head'
import { addRoomy } from '@/apis/tenant/roomy'
export default {
  components: {
    'hunk-head': Head
  },
  data() {
    return {
      addInfo: {
        roommatename: '',
        roommatecardid: '',
        isanonymous:0
      },
      isanonymous: [
        {"value": 0,"text": "否"},
        {"value": 1,"text": "是"}
      ],
      userInfo: null,
      wid:0,
      
    }
  },
  watch:{
    'addInfo.isanonymous'(news){
       if(+news===1){
        this.addInfo.roommatename = '匿名'
        this.addInfo.roommatecardid = '111111191111111113'
      }else{
        this.addInfo.roommatename = ''
        this.addInfo.roommatecardid = ''
      }
    }
  },
  onLoad(e){
    console.log(e.query)
    this.userInfo = JSON.parse(e.query)
  },
  computed:{
    unickname(){
      return this.$store.state.baseStore.userInfo.unickname
    },
    userid(){
      return this.$store.state.baseStore.userInfo.userid
    },
    isbtn(){
      if(this.addInfo.roommatename == '' || this.addInfo.roommatecardid == ''){
        return false
      }else{
        let reg = /^(^\d{18}$|^\d{17}(\d|X|x))$/
        if(!reg.test(this.addInfo.roommatecardid)){
          
          return false
        }else{
          return true
        }
      }
    }
  },
  onShareAppMessage(res) {
    if (res.from === 'button') {
      this.$refs.popup.close()
      console.log(JSON.stringify({...this.addInfo,outtype:this.userInfo.outtype,address:this.userInfo.address,widnew: this.wid,uniname:this.unickname}))
      return {
        title: this.unickname+'邀请您入住'+this.userInfo.address,
        path: '/pages/tenant/roomy/addlink?query=' + JSON.stringify({wid: this.wid})
      }
    }
  },
  methods: {
    add(){
      console.log('dddd')
    },
    addRoomy(){
      if(this.addInfo.roommatename == '' || this.addInfo.roommatecardid == ''){
        uni.showToast({
          title: '请填写室友信息',
          icon: 'none'
        })
      }else{
        let reg = /^(^\d{18}$|^\d{17}(\d|X|x))$/
        if(!reg.test(this.addInfo.roommatecardid)){
          uni.showToast({
            title: '请正确输入身份证',
            icon: 'none'
          })
          return
        }
        const data = {
          "houseid": this.userInfo.houseid,
          "houseroomid": this.userInfo.houseroomid,
          "roommatecardid": this.addInfo.roommatecardid,
          "roommatename": this.addInfo.roommatename,
          "isanonymous": this.addInfo.isanonymous,
          "userid": this.userid,
          "wid": 0,
          "contractid":this.userInfo.wid
        }
        let that = this
        addRoomy(data).then((res)=>{
          if(res.code == 200){
            that.wid = res.data.wid
            that.$refs.popup.open()
          }else{
            uni.showToast({
              title: res.msg,
              icon: 'error'
            })
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.addroomy {
  width: 100vw;
  box-sizing: border-box;
  height: 100vh;
  position: relative;
  background: #F9F9F9;
  .addform{
    background: #fff;
    .form-item{
      padding: 12px 15px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .name{
        font-size: 15px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #000000;
        line-height: 21px;
      }
      .add-input{
        input{
          font-size: 14px;
          font-family: PingFangSC-Medium, PingFang SC;
          font-weight: 500;
          color: #4a4a4a;
          line-height: 20px;
          text-align: right;
        }
        input::placeholder{
          color: #9B9B9B;
        }
      }
    }
    .space{
      margin-left: 10px;
      height: 2px;
      border: 1px solid #EFEFEF;
    }
  }
  .addbtn{
    position: absolute;
    bottom: 40px;
    left: 0;
    right: 0;
    margin: auto;
    width: 355px;
    height: 45px;
    background: linear-gradient(315deg, #FFAA58 0%, #FFBD6C 100%);
    box-shadow: 0px 2px 4px 0px rgba(252, 168, 55, 0.21), 0px 3px 5px 0px rgba(255, 175, 111, 0.14);
    border-radius: 100px;
    text-align: center;
    font-size: 16px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #FFFFFF;
    line-height: 45px;
    text-shadow: 0px 2px 4px rgba(252, 168, 55, 0.21);
  }
  .popup1{
  width: 335px;
  height: 212px;
  background: #FFFFFF;
  display: flex;
  flex-direction: column;
  align-items: center;
  border-radius: 10px;
  .con-head{
    width: 100%;
    font-size: 18px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #4A4A4A;
    line-height: 25px;
    position: relative;
    box-sizing: border-box;
    padding: 18px 0;
    text-align: center;
    border-bottom: 1px solid #EFEFEF;
    img{
      position: absolute;
      right: 15px;
      width: 18px;
      height: 18px;
    }
  }
  .btn1{
    margin-top: 20px;
    width: 305px;
    height: 45px;
    background: linear-gradient(315deg, #FFAA58 0%, #FFBD6C 100%);
    border-radius: 100px;
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFFFFF;
    line-height: 45px;
    text-align: center;
  }
  .btn2{
    margin-top: 20px;
    width: 305px;
    height: 45px;
    // background: linear-gradient(140deg, #FFCF58 0%, #FFD758 100%);
    background: rgba(130, 130, 130, 0.2);
    border-radius: 100px;
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFFFFF;
    line-height: 45px;
    text-align: center;
  }
  button::after{
    border: 0;
  }
}
}


</style>